//login.js
//获取应用实例
const app = getApp()
var util =  require('../../../utils/healthUtil.js');
var json =  require('../../../data/healthJson.js');
Page({
  
  data: {
    array: ["男", "女"],
    index: '',
    sex:'',
    date:'',
    stature: '',
    downImg: '/resources/healthData/down.png',
    upImg: '/resources/healthData/up.png',
    btnTxt:{
      btnTxt:'下一步'
    },
    userInfo: {},
    isHeight:false,
    styles: {
      line: '#d0d0d0',
      bginner: '#fbfbfb',
      bgoutside: '#ffffff',
      lineSelect: '#2897ff',
      font: '#000000'
    },
    isOne: true
  },
  
  //picker
  bindSexChange: function (e) {
    this.setData({
      index: e.detail.value,
      sex: this.data.array[e.detail.value],
      sexImgsrc: this.data.downImg,
    })
  },

  bindSexCancel:function(){
    this.setData({
      sexImgsrc: this.data.downImg
    })
  },

  bindDateChange: function(e) {
    this.setData({
      date: e.detail.value,
      dateImgsrc: this.data.downImg
    })
  },

  bindDateCancel:function(){
    this.setData({
      dateImgsrc: this.data.downImg
    })
  },
  
  //选中的身高
  bindvalue: function (e) {
    this.setData({
      value: e.detail.value,
    })
  },

  //选择箭头
  imgChange:function(e)
  {
    var that = this
    if(e.currentTarget.dataset.type == "性别")
    {
      if(that.data.sexImgsrc == that.data.downImg)
      {
        that.setData({
          sexImgsrc: that.data.upImg
        })
      }
    }
    else if(e.currentTarget.dataset.type == "日期"){
      if(that.data.dateImgsrc == that.data.downImg)
      {
        that.setData({
          dateImgsrc: that.data.upImg
        })
      }
    }
  },

  //初始体重
  startInput:function(e){
    this.setData({
      start : e.detail.value
    })
  },

  //目标体重
  targetInput:function(e){
    this.setData({
      target : e.detail.value
    })
  },

  onLoad: function () {
    // console.log(app.globalData.userInfo)
    var that = this
    if (!app.globalData.userInfo) {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      app.userInfoReadyCallback = res => {
        if (res) {
          that.setData({
            userInfo: res.userInfo
          });
        }
      }
    }else{
      this.setData({
        userInfo: app.globalData.userInfo
      })
    }
    if(app.globalData.userId){
      that.setData({
        userId: app.globalData.userId
      })
    }else{
      app.userIdCallback = userId => {
        if (userId != '') {
          that.setData({
            userId: userId
          });
        }
      }
    }
    that.setData({
      heightImgsrc: that.data.downImg,
      sexImgsrc: that.data.downImg,
      dateImgsrc: that.data.downImg,
      moduleList: json.moduleList,
    })
  },

  //选择身高
  selectHeight: function (e) {
    var currentStatu = e.currentTarget.dataset.statu;
    this.anim(currentStatu)
  },

  //动画部分
  anim: function (currentStatu) {
    // 第1步：创建动画实例 
    var animation = wx.createAnimation({
      duration: 200,  //动画时长
      timingFunction: "linear", //线性
      delay: 0  //0则不延迟
    });
    
    // 第2步：这个动画实例赋给当前的动画实例
    this.animation = animation;
 
    // 第3步：执行第一组动画：Y轴偏移240px后(盒子高度是240px)，停
    animation.translateY(280).step();
 
    // 第4步：导出动画对象赋给数据对象储存
    this.setData({
      animationData: animation.export()
    })
    
    // 第5步：设置定时器到指定时候后，执行第二组动画
    setTimeout(function () {
      // 执行第二组动画：Y轴不偏移，停
      animation.translateY(0).step()
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象
      this.setData({
        animationData: animation
      })
      
      //完成
      if (currentStatu == "complete") {
        this.setData(
          {
            isHeight: false,
            stature: this.data.value,
            heightImgsrc: this.data.downImg
          }
        );
      }

      //取消
      else if (currentStatu == "cancel") {
        this.setData(
          {
            isHeight: false,
            heightImgsrc: this.data.downImg
          }
        );
      }
    }.bind(this), 200)
  
    // 显示抽屉
    if (currentStatu == "open") {
      this.setData(
        {
          isHeight: true,
          heightImgsrc: this.data.upImg
        }
      );
    }
  },

  //阻止穿透
  preventTouchMove: function () {
    // console.log('stop user scroll it!');
    return;
  },

  //选择模块
  chageModule:function(e){
    var that = this
    var index = e.currentTarget.dataset.index;
    var list = that.data.moduleList
    for(var i in list){
      if(index == list[i].index){
        list[i].isShow = !list[i].isShow
      }
    }
    that.setData({
      moduleList: list
    })
  },

  //判断值
  judgmentValue:function(){
    var that = this
    // if(that.data.sex == ''){
    //   util.showModal('提示','请选择性别')
    //   return false;
    // }
    if(that.data.stature == '')
    {
      util.showModal('提示','请选择身高')
      return false;
    }
    // if(that.data.date == '')
    // {
    //   util.showModal('提示','请选择出生日期')
    //   return false;
    // }
    if(that.data.start){
      let isStart = util.validation(that.data.start,'number')
      if(!isStart){
        util.showModal('提示','初始体重输入不正确')
        return false;
      }
    }else{
      util.showModal('提示','请输入初始体重')
      return false;
    }
    if(that.data.target){
      let isTarget = util.validation(that.data.target,'number')
      if(!isTarget){
        util.showModal('提示','目标体重输入不正确')
        return false;
      }
    }else{
      util.showModal('提示','请输入目标体重')
      return false;
    }
    return true
  },

  //判断模块
  judgmentModule:function(){
    var that = this
    var list = that.data.moduleList
    var check = []
    var uncheck = []
    for(var i in list){
      if(list[i].isShow){
        check.push(list[i].index)
      }else{
        uncheck.push(list[i].index)
      }
    }
    that.setData({
      check: check,
      uncheck: uncheck
    })
  },
  
  //模块全选
  allModule:function(){
    var that = this
    that.setData({
      isAll: true
    })
    that.AllCheckModule()
    that.lickBtn()
  },

  //全选模块
  AllCheckModule:function(){
    var that = this
    var list = that.data.moduleList
    var check = []
    var uncheck = []
    for(var i in list){
      check.push(list[i].index)
    }
    that.setData({
      check: check,
      uncheck: []
    })
  },

  lickBtn: function () {
    var that = this
    if(!that.judgmentValue()) return
    if(that.data.isOne){
      //下一步
      var btnTxt = 'btnTxt.btnTxt';
      that.setData({
        isOne: false,
        [btnTxt] : '完成',
      })
    }else{
      //保存信息
      that.setData({
        btnDis:true
      })
      if(!that.data.isAll){
        that.judgmentModule();
      }
      // console.log(that.data.check)
      wx.request({
        url: app.globalData.apiUrl + '/add_user',
        data: {
          id: that.data.userId,
          height: that.data.stature,
          startweight: that.data.start,
          targetweight: that.data.target,
          is_show: that.data.check,
          is_head: that.data.uncheck,
          is_qx: that.data.isAll?0:1
        },
        method: 'post',
        header: {
          'content-type': 'application/x-www-form-urlencoded' //默认值
        },
        success: function (res) {
          // console.log(res)
          util.showToast('保存成功','success')
          // app.globalData.userId = res.data.userid;
          // app.globalData.isOne = true
          wx.redirectTo({
            url: '/subPages/health/healthData/index/index',
          })
        },
        fail: function (res) {
          wx.showModal({
            title: '提示',
            content: '进入失败，请重试进入',
            showCancel: false,
            complete (res) {
              that.setData({
                btnDis: false
              })
            }
          })
        }
      })
    }
  },
})